using System.Collections;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class TitanControlls : MonoBehaviour
{
	public static TitanControlls instance;

	private TitanBehavior titan;

	public ClickCallback shootMainWeaponsButton;

	public ClickCallback shootAdditionalWeaponsButton;

	public ClickCallback shootAllWeaponsButton;

	public ClickCallback useJetpack;

	public ReloadingUI mainWeaponsReloadingUI;

	public ReloadingUI additionalWeaponsReloadingUI;

	public Coroutine mainWeaponsReloadingCoroutine;

	public Coroutine additionalWeaponsReloadingCoroutine;

	public UISprite jetpackFuel;

	public UISprite mainWeaponAmmoBar;

	public UISprite additionalWeaponAmmoBar;

	public UILabel mainWeaponAmmoTime;

	public UILabel additionalWeaponAmmoTime;

	public UISprite aimSprite;

	public GameObject mainWeaponsButton;

	public GameObject mainWeaponsButtonInactive;

	public GameObject additionalWeaponsButton;

	public GameObject additionalWeaponsButtonInactive;

	private Tweener hitAnimation;

	private void Awake()
	{
		instance = this;
	}

	public void HitAnimation()
	{
		if (hitAnimation != null)
		{
			HOTween.Kill(hitAnimation);
		}
		aimSprite.color = Color.red;
		hitAnimation = HOTween.To(aimSprite, 0.2f, new TweenParms().Prop("color", Color.white).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
		{
		}));
	}

	public void StartControlls(TitanBehavior titan)
	{
		if (!(this.titan == titan))
		{
			StopControlls();
			this.titan = titan;
		}
	}

	private void OnEnable()
	{
		shootMainWeaponsButton.click = ShootMainWeapons;
		shootAdditionalWeaponsButton.click = ShootAdditionalWeapons;
		shootAllWeaponsButton.click = ShootAllWeapons;
		useJetpack.click = UseJetpack;
	}

	private void OnDisable()
	{
		StopControlls();
	}

	public void ShootAllWeapons(bool pressed)
	{
		ShootMainWeapons(pressed);
		ShootAdditionalWeapons(pressed);
	}

	public void ShootMainWeapons(bool pressed)
	{
		if (titan == null)
		{
			return;
		}
		if (titan.mainWeaponsIsHoming && !titan.TargetDetector.enemyCatchedForHomingMissle)
		{
			titan.mainWeaponsShooting = false;
			CameraZoneEnabler.GetCameraZone().PressFromButton(false);
			return;
		}
		if (!pressed && titan.cycleShootAnimation && titan.alreadyShootingMainWeapons)
		{
			titan.topMecanimWrapper.SetTrigger("ShootEnd");
		}
		titan.mainWeaponsShooting = pressed;
		CameraZoneEnabler.GetCameraZone().PressFromButton(pressed);
	}

	public void ShootAdditionalWeapons(bool pressed)
	{
		if (titan == null)
		{
			return;
		}
		if (titan.additionalWeaponsIsHoming && !titan.TargetDetector.enemyCatchedForHomingMissle)
		{
			titan.repeatAdditionalWeaponsShooting = false;
			CameraZoneEnabler.GetCameraZone().PressFromButton(false);
			return;
		}
		if (pressed)
		{
			titan.StartShootingAdditionalWeapons();
		}
		titan.repeatAdditionalWeaponsShooting = pressed;
		CameraZoneEnabler.GetCameraZone().PressFromButton(pressed);
	}

	public void UseJetpack(bool pressed)
	{
		if (!(titan == null))
		{
			titan.titanController.usingJetpack = pressed;
			CameraZoneEnabler.GetCameraZone().PressFromButton(pressed);
		}
	}

	public void StopControlls()
	{
		if (!(titan == null))
		{
			titan.mainWeaponsShooting = false;
			titan.additionalWeaponsShooting = false;
			titan.titanController.usingJetpack = false;
			titan.repeatAdditionalWeaponsShooting = false;
			mainWeaponAmmoTime.gameObject.SetActive(false);
			additionalWeaponAmmoTime.gameObject.SetActive(false);
			titan = null;
		}
	}

	private void Update()
	{
		if (!(titan == null))
		{
			UpdateJetPackFuel();
			if (titan.mainWeapons[0] != null && titan.mainWeapons[0].weaponTypeSpecific != WeaponTypeSpecific.Trebuchet)
			{
				UpdateMainWeaponAmmo();
			}
			else
			{
				UpdateChargeMainWeapon();
			}
			UpdateAdditionalWeaponAmmo();
			SetButtons();
		}
	}

	private void SetButtons()
	{
		if (titan.mainWeapons.Count == 0)
		{
			DisableMainWeaponsInterface();
		}
		else if (titan.mainWeaponsIsHoming && !titan.TargetDetector.enemyCatchedForHomingMissle)
		{
			mainWeaponsButton.SetActive(false);
			mainWeaponsButtonInactive.SetActive(true);
			titan.mainWeaponsShooting = false;
			CameraZoneEnabler.GetCameraZone().PressFromButton(false);
		}
		else
		{
			mainWeaponsButton.SetActive(true);
			mainWeaponsButtonInactive.SetActive(false);
		}
		if (titan.additionalWeapons.Count == 0)
		{
			DisableAdditionalWeaponsInterface();
		}
		else if (titan.additionalWeaponsIsHoming && !titan.TargetDetector.enemyCatchedForHomingMissle)
		{
			additionalWeaponsButton.SetActive(false);
			additionalWeaponsButtonInactive.SetActive(true);
			titan.repeatAdditionalWeaponsShooting = false;
			CameraZoneEnabler.GetCameraZone().PressFromButton(false);
		}
		else
		{
			additionalWeaponsButton.SetActive(true);
			additionalWeaponsButtonInactive.SetActive(false);
		}
	}

	private void DisableAdditionalWeaponsInterface()
	{
		additionalWeaponsReloadingUI.gameObject.SetActive(false);
		additionalWeaponAmmoBar.gameObject.SetActive(false);
		additionalWeaponsButton.SetActive(false);
		additionalWeaponsButtonInactive.SetActive(false);
	}

	private void DisableMainWeaponsInterface()
	{
		mainWeaponsReloadingUI.gameObject.SetActive(false);
		mainWeaponAmmoBar.gameObject.SetActive(false);
		mainWeaponsButton.SetActive(false);
		mainWeaponsButtonInactive.SetActive(false);
	}

	public void StartReloadMainAmmo(float reloadTime)
	{
		if (mainWeaponsReloadingCoroutine != null)
		{
			StopCoroutine(mainWeaponsReloadingCoroutine);
			mainWeaponsReloadingCoroutine = null;
			mainWeaponAmmoTime.gameObject.SetActive(false);
		}
		if (titan.mainWeapons[0].weaponTypeSpecific != WeaponTypeSpecific.Trebuchet)
		{
			mainWeaponsReloadingUI.ReloadAnimation(reloadTime);
		}
		mainWeaponsReloadingCoroutine = StartCoroutine(TimeReloadCoroutine(reloadTime, mainWeaponAmmoTime));
	}

	public void StartReloadAdditionalAmmo(float reloadTime)
	{
		if (additionalWeaponsReloadingCoroutine != null)
		{
			StopCoroutine(additionalWeaponsReloadingCoroutine);
			additionalWeaponsReloadingCoroutine = null;
			additionalWeaponAmmoTime.gameObject.SetActive(false);
		}
		additionalWeaponsReloadingUI.ReloadAnimation(reloadTime);
		additionalWeaponsReloadingCoroutine = StartCoroutine(TimeReloadCoroutine(reloadTime, additionalWeaponAmmoTime));
	}

	public void StopReloadAnimation()
	{
		mainWeaponsReloadingUI.gameObject.SetActive(false);
		additionalWeaponsReloadingUI.gameObject.SetActive(false);
		mainWeaponAmmoTime.gameObject.SetActive(false);
		additionalWeaponAmmoTime.gameObject.SetActive(false);
		if (mainWeaponsReloadingCoroutine != null)
		{
			StopCoroutine(mainWeaponsReloadingCoroutine);
			mainWeaponsReloadingCoroutine = null;
		}
		if (additionalWeaponsReloadingCoroutine != null)
		{
			StopCoroutine(additionalWeaponsReloadingCoroutine);
			additionalWeaponsReloadingCoroutine = null;
		}
	}

	private IEnumerator TimeReloadCoroutine(float reloadTime, UILabel labelTime)
	{
		labelTime.gameObject.SetActive(true);
		float seconds = reloadTime;
		if (seconds != Mathf.Floor(seconds))
		{
			float step = seconds - Mathf.Floor(seconds);
			seconds -= step;
			labelTime.text = seconds.ToString();
			yield return new WaitForSeconds(step);
		}
		while (seconds > 0f)
		{
			labelTime.text = seconds.ToString();
			seconds -= 1f;
			yield return new WaitForSeconds(1f);
		}
		labelTime.gameObject.SetActive(false);
		yield return null;
	}

	private void UpdateMainWeaponAmmo()
	{
		mainWeaponAmmoBar.fillAmount = titan.MainWeaponsCurrentTimeToReload / titan.mainWeaponsMaxTimeToReload;
	}

	private void UpdateAdditionalWeaponAmmo()
	{
		additionalWeaponAmmoBar.fillAmount = titan.AdditionalWeaponsCurrentTimeToReload / titan.additionalWeaponsShootingTime;
	}

	private void UpdateJetPackFuel()
	{
		jetpackFuel.fillAmount = titan.jetpackFuel / 100f;
		if (jetpackFuel.fillAmount != 1f)
		{
			jetpackFuel.alpha = Mathf.Lerp(jetpackFuel.alpha, 0.5f, Time.deltaTime * 5f);
		}
		else
		{
			jetpackFuel.alpha = Mathf.Lerp(jetpackFuel.alpha, 0f, Time.deltaTime * 5f);
		}
	}

	private void UpdateChargeMainWeapon()
	{
		if (titan.mainWeapons[0] != null)
		{
			mainWeaponAmmoBar.fillAmount = titan.mainWeapons[0].PercentAccumulationCharge();
		}
	}
}
